//
//  DataBaseTool.m
//  OurMenu
//
//  Created by 尹坤 on 15/10/13.
//  Copyright © 2015年 尹坤. All rights reserved.
//

#import "DataBaseTool.h"
#import "FMDatabase.h"
#import "DishesMadels.h"
#import "Steps.h"
@implementation DataBaseTool
- (instancetype)init
{
    self = [super init];
    if (self) {
        NSString *foodPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
        NSString *dbPath = [foodPath stringByAppendingPathComponent:@"foods.sqlite"];
        db = [FMDatabase databaseWithPath:dbPath];
       // NSLog(@"%@",NSHomeDirectory());
        
    }
    return self;
}
- (void)creatDishesTable
{
    if ([db open]) {
        NSString *createSql = [NSString stringWithFormat:@"create table if not exists dish (id integer primary key autoincrement,title text,Aalbums text,tags text,imtro text,ingredients text,burden text)"];
        BOOL result = [db executeUpdate:createSql];
        if (result) {
            //NSLog(@"建表成功");
        }else{
            //NSLog(@"建表失败");
        }
    }
    [db close];
}
- (void)creatSteps
{
    if ([db open]) {
        NSString *createSql = [NSString stringWithFormat:@"create table if not exists step (id integer primary key autoincrement,img text,step text,title text)"];
        BOOL result = [db executeUpdate:createSql];
        if (result) {
            //NSLog(@"建表成功");
        }else{
           // NSLog(@"建表失败");
        }
    }
    [db close];
}
- (BOOL)insertDishesToTable:(DishesMadels *)dishes
{
    if ([db open]) {
        NSString *insertSql = [NSString stringWithFormat:@"insert into dish (title,Aalbums,tags,imtro,ingredients,burden) values ('%@','%@','%@','%@','%@','%@')",dishes.title,dishes.Aalbums,dishes.tags,dishes.imtro,dishes.ingredients,dishes.burden];
        BOOL result = [db executeUpdate:insertSql];
        if (result) {
            [db close];
            return YES;
        }
    }
    [db close];
    return NO;
}
- (BOOL)insertStepsToTable:(Steps *)step
{
    if ([db open]) {
        NSString *insertSql = [NSString stringWithFormat:@"insert into step (title,img,step) values ('%@','%@','%@')",step.title,step.img,step.step];
        BOOL result = [db executeUpdate:insertSql];
        if (result) {
            [db close];
            return YES;
        }
    }
    [db close];
    return NO;
}
- (BOOL)isExistsWithName:(NSString *)name
{
    if ([db open]) {
        NSString *selectSql = [NSString stringWithFormat:@"select * from dish where title = '%@'",name];
        FMResultSet *set = [db executeQuery:selectSql];
        while ([set next]) {
            NSString *nameResult = [set stringForColumn:@"title"];
            if ([nameResult isEqualToString:name]) {
                [db close];
                return YES;
            }
        }
    }
    [db close];
    return NO;
}
- (BOOL)isExistsWithStepsName:(NSString *)name
{
    if ([db open]) {
        NSString *selectSql = [NSString stringWithFormat:@"select * from step where title = '%@'",name];
        FMResultSet *set = [db executeQuery:selectSql];
        while ([set next]) {
            NSString *nameResult = [set stringForColumn:@"title"];
            if ([nameResult isEqualToString:name]) {
                [db close];
                return YES;
            }
        }
    }
    [db close];
    return NO;
}
- (BOOL)deleteDishes:(NSString *)name
{
    if ([db open]) {
        NSString *deleteSql = [NSString stringWithFormat:@"delete from dish where title = '%@'",name];
        BOOL result = [db executeUpdate:deleteSql];
        if (result) {
            [db close];
            return YES;
        }
    }
    [db close];
    return NO;
}
- (BOOL)deleteSteps:(NSString *)title
{
    if ([db open]) {
        NSString *deleteSql = [NSString stringWithFormat:@"delete from step where title = '%@'",title];
        BOOL result = [db executeUpdate:deleteSql];
        if (result) {
            [db close];
            return YES;
        }
    }
    [db close];
    return NO;
}
- (NSMutableArray *)selectDishes
{
    NSMutableArray *array = [NSMutableArray array];
    if ([db open]) {
        NSString *selectSql = [NSString stringWithFormat:@"select * from dish"];
        FMResultSet *set = [db executeQuery:selectSql];
        while ([set next]) {
            DishesMadels *dish = [[DishesMadels alloc] init];
            dish.title = [set stringForColumn:@"title"];
            dish.tags = [set stringForColumn:@"tags"];
            dish.Aalbums = [set stringForColumn:@"Aalbums"];
            dish.imtro = [set stringForColumn:@"imtro"];
            dish.ingredients = [set stringForColumn:@"ingredients"];
            dish.burden = [set stringForColumn:@"burden"];
           
            [array addObject:dish];
        }
    }
    [db close];
    return array;
}
- (NSMutableArray *)selectSteps:(NSString *)name
{
    NSMutableArray *array = [NSMutableArray array];
    if ([db open]) {
        NSString *selectSql = [NSString stringWithFormat:@"select * from step where title = '%@'",name];
        FMResultSet *set = [db executeQuery:selectSql];
        while ([set next]) {
            Steps *step = [[Steps alloc] init];
            step.title = [set stringForColumn:@"title"];
            step.img = [set stringForColumn:@"img"];
            step.step = [set stringForColumn:@"step"];
            [array addObject:step];
        }
    }
    [db close];
    return array;
}

@end
